<% contract_creation_code = contract_creation_code(@address) %>
<% minimal_proxy_template = Chain.get_minimal_proxy_template(@address.hash) %>
<% metadata_for_verification = minimal_proxy_template || Chain.get_address_verified_twin_contract(@address.hash).verified_contract %>
<% smart_contract_verified = BlockScoutWeb.AddressView.smart_contract_verified?(@address) %>
<% additional_sources_from_twin = Chain.get_address_verified_twin_contract(@address.hash).additional_sources %>
<% fully_verified = Chain.smart_contract_fully_verified?(@address.hash)%>
<% additional_sources = if smart_contract_verified, do: @address.smart_contract_additional_sources, else: additional_sources_from_twin %>
<% visualize_sol2uml_enabled = Explorer.Visualize.Sol2uml.enabled?() %>
<section class="container">
  <% is_proxy = BlockScoutWeb.AddressView.smart_contract_is_proxy?(@address) %>

  <%= render BlockScoutWeb.AddressView, "overview.html", address: @address, is_proxy: is_proxy, conn: @conn, exchange_rate: @exchange_rate, coin_balance_status: @coin_balance_status, counters_path: @counters_path, tags: @tags %>

  <div class="card">
    <%= render BlockScoutWeb.AddressView, "_tabs.html", address: @address, is_proxy: is_proxy, conn: @conn %>
    <div class="card-body">
      <%= unless smart_contract_verified do %>
        <%= if minimal_proxy_template do %>
          <%= render BlockScoutWeb.CommonComponentsView, "_minimal_proxy_pattern.html", address_hash: metadata_for_verification.address_hash, conn: @conn %>
        <% else %>
          <%= if metadata_for_verification do %>
            <% path = address_verify_contract_path(@conn, :new, @address.hash) %>
            <div class="mb-4">
              <div style="display: inline-block;">
                <%= render BlockScoutWeb.CommonComponentsView, "_info.html" %>
                <span> <%= gettext("Contract is not verified. However, we found a verified contract with the same bytecode in Blockscout DB") %> <%= link(
                  metadata_for_verification.address_hash,
                  to: address_contract_path(@conn, :index, metadata_for_verification.address_hash)) %>.<br/> <%= gettext("All metadata displayed below is from that contract. In order to verify current contract, click") %> <i><%= gettext("Verify & Publish") %></i> <%= gettext("button") %></span>
              </div>
              <%= link(gettext("Verify & Publish"), to: path, class: "button button-primary button-sm float-right ml-3", "data-test": "verify_and_publish") %>
            </div>
          <% end %>
        <% end %>
      <% end %>
      <%= if smart_contract_verified do %>
        <%= if @address.smart_contract.is_changed_bytecode do %>
          <%= render BlockScoutWeb.CommonComponentsView, "_changed_bytecode_warning.html" %>
        <% else %>
          <div class="d-none" data-selector="hidden-bytecode-warning">
            <%= render BlockScoutWeb.CommonComponentsView, "_changed_bytecode_warning.html" %>
          </div>
        <% end %>
      <% end %>
      <%= if smart_contract_verified || (!smart_contract_verified && metadata_for_verification) do %>
        <% target_contract = if smart_contract_verified, do: @address.smart_contract, else: metadata_for_verification %>
        <%= if @address.smart_contract.partially_verified && smart_contract_verified do %>
          <div class="mb-4">
            <i style="color: #f7b32b;" class="fa fa-info-circle"></i><span> <%= gettext("This contract has been partially verified via Sourcify.") %>
        <% else %>
          <%= if @address.smart_contract.verified_via_sourcify && smart_contract_verified do %>
            <div class="mb-4">
              <i style="color: #f7b32b;" class="fa fa-info-circle"></i><span> <%= gettext("This contract has been verified via Sourcify.") %>
          <% end %>
        <% end %>
        <%= if @address.smart_contract.verified_via_sourcify && smart_contract_verified do %>
          <a data-test="external_url" href=<%= sourcify_repo_url(@address.hash, @address.smart_contract.partially_verified) %> target="_blank">
            View contract in Sourcify repository <span class="external-token-icon"><%= render BlockScoutWeb.IconsView, "_external_link.html" %></span>
          </a>
        </div>

        <% end %>
        <div class="mb-4">
          <dl class="row">
            <dt class="col-md-2 text-muted"><%= gettext "Contract name:" %></dt>
            <dd class="col-md-4"><%= target_contract.name %></dd>
            <div class="d-none d-sm-block d-md-none"></br></br></div>
            <div class="d-block d-sm-none"></br></br></div>
            <dt class="col-md-2 text-muted"><%= gettext "Optimization enabled" %></dt>
            <dd class="col-md-4"><%= if target_contract.is_vyper_contract, do: "N/A", else: format_optimization_text(target_contract.optimization) %></dd>
          </dl>
          <dl class="row">
            <dt class="col-md-2 text-muted"><%= gettext "Compiler version" %></dt>
            <dd class="col-md-4"><%= target_contract.compiler_version %></dd>
            <div class="d-none d-sm-block d-md-none"></br></br></div>
            <div class="d-block d-sm-none"></br></br></div>
            <%= if target_contract.optimization && target_contract.optimization_runs do %>
              <dt class="col-md-2 text-muted"><%= gettext "Optimization runs" %></dt>
              <dd class="col-md-4"><%= target_contract.optimization_runs %></dd>
            <% end %>
          </dl>
          <dl class="row">
            <%= if smart_contract_verified && target_contract.evm_version do %>
              <dt class="col-md-2 text-muted"><%= gettext "EVM Version" %></dt>
              <dd class="col-md-4"><%= target_contract.evm_version %></dd>
              <div class="d-none d-sm-block d-md-none"></br></br></div>
              <div class="d-block d-sm-none"></br></br></div>
            <% end %>
            <%= if target_contract.inserted_at do %>
              <dt class="col-md-2 text-muted"><%= gettext "Verified at" %></dt>
              <dd class="col-md-4"><%= target_contract.inserted_at %></dd>
            <% end %>
          </dl>
        <hr/>
        <%= if smart_contract_verified && target_contract.constructor_arguments do %>
          <section>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "Constructor Arguments" %></h3>
            </div>
            <div class="tile tile-muted mb-4">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= raw(format_constructor_arguments(target_contract, @conn)) %></code>
              </pre>
            </div>
          </section>
        <% end %>
        <section>
          <div class="d-flex justify-content-between align-items-baseline">
            <h3><%= target_contract.file_path || gettext "Contract source code" %></h3>
            <div class="buttons">
              <%= if visualize_sol2uml_enabled && !target_contract.is_vyper_contract && !is_nil(target_contract.abi) do %>
                <span data-original-title="Visualize contract code using Sol2Uml JS library" data-placement="top" data-toggle="tooltip">
                  <a class="button button-primary button-sm" href="<%= visualize_sol2uml_path(@conn, :index, address: @address) %>">
                      <div class="d-flex align-items-center">
                        <i class="fa-solid fa-diagram-project mr-1"></i>
                        <span class="mr-1">Sol2uml</span>
                        <div class="badge badge-info">new</div>
                      </div>
                  </a>
                </span>
                <% end %>
                <button type="button" class="btn-line ml-3 float-right" id="button" data-toggle="tooltip" data-placement="top" data-clipboard-text="<%= target_contract.contract_source_code %>" aria-label="Copy Contract Source Code">
                  <%= gettext "Copy Source Code" %>
                </button>
            </div>
          </div>
          <pre class="tile-muted tile-code mb-4" id="code_viewer_main" data-additional-sources-length=<%= Enum.count(additional_sources) %>><%= target_contract.contract_source_code %><pre>
        </section>

        <%= additional_sources |> Enum.with_index() |> Enum.map(fn {additional_source, index} -> %>
          <section>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= additional_source.file_name %></h3>
              <button type="button" class="btn-line" id="button" data-toggle="tooltip" data-placement="top" data-clipboard-text="<%= additional_source.contract_source_code %>" aria-label="Copy Contract Source Code">
                <%= gettext "Copy Source Code" %>
              </button>
            </div>
            <pre class="tile-muted tile-code mb-4" id="code_viewer_<%= index %>"><%= additional_source.contract_source_code %><pre>
          </section>
        <% end)%>

        <%= if !is_nil(target_contract.compiler_settings) do %>
          <section>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "Compiler Settings" %></h3>
              <button type="button" class="btn-line" id="button" data-clipboard-text="<%= format_smart_contract_abi(target_contract.compiler_settings) %>" aria-label="Copy Compiler Settings">
                <%= gettext "Copy Compiler Settings" %>
              </button>
            </div>
            <div class="tile tile-muted mb-4">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= format_smart_contract_abi(target_contract.compiler_settings) %></code>
              </pre>
            </div>
          </section>
        <% end %>

        <%= if !is_nil(target_contract.abi) do %>
          <section>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "Contract ABI" %></h3>
              <button type="button" class="btn-line" id="button" data-clipboard-text="<%= format_smart_contract_abi(target_contract.abi) %>" aria-label="Copy Contract ABI">
                <%= gettext "Copy ABI" %>
              </button>
            </div>
            <div class="tile tile-muted mb-4">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= format_smart_contract_abi(target_contract.abi) %></code>
              </pre>
            </div>
          </section>
        <% end %>

      <% end %>
      <section>
        <%= case contract_creation_code do %>
          <% {:selfdestructed, transaction_init} -> %>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "Contract Creation Code" %></h3>
              <button type="button" class="btn-line" id="button" data-clipboard-text="<%= transaction_init %>" aria-label="copy contract creation code">
                <%= gettext "Copy Contract Creation Code" %>
              </button>
            </div>
            <div class="alert alert-info">
              <p><%= gettext "Contracts that self destruct in their constructors have no contract code published and cannot be verified." %></p>
              <p><%= gettext "Displaying the init data provided of the creating transaction." %></p>
            </div>
            <div class="tile tile-muted">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= transaction_init %></code></pre>
            </div>
          <% {:ok, contract_code} -> %>
            <%= if creation_code(@address) do %>
              <div class="d-flex justify-content-between align-items-baseline">
                <h3><%= gettext "Contract Creation Code" %></h3>
                <div class="buttons">
                  <button type="button" class="btn-line" style="float: left;" id="button" data-clipboard-text="<%= creation_code(@address) %>" aria-label="copy contract creation code">
                    <%= gettext "Copy Contract Creation Code" %>
                  </button>
                  <%= if !fully_verified do %>
                    <% path = address_verify_contract_path(@conn, :new, @address.hash) %>
                    <%= link(
                          gettext("Verify & Publish"),
                          to: path,
                          class: "button button-primary button-sm float-right ml-3",
                          "data-test": "verify_and_publish"
                        ) %>
                  <% end %>
                </div>
              </div>
              <div class="tile tile-muted mb-4">
                <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= creation_code(@address) %></code></pre>
              </div>
            <% end %>
            <%= if fully_verified do %>
              <div class="d-flex justify-content-between align-items-baseline">
                <h3><%= gettext "Deployed ByteCode" %></h3>
                <button type="button" class="btn-line" style="float: left;" id="button" data-clipboard-text="<%= contract_code %>" aria-label="copy contract creation code">
                  <%= gettext "Copy Deployed ByteCode" %>
                </button>
              </div>
            <% else %>
              <div class="d-flex justify-content-between align-items-baseline">
                <div>
                  <h3><%= gettext "Deployed ByteCode" %></h3>
                </div>
                <div class="buttons">
                  <button type="button" class="btn-line" style="float: left;" id="button" data-clipboard-text="<%= contract_code %>" aria-label="copy contract creation code">
                    <%= gettext "Copy Deployed ByteCode" %>
                  </button>
                  <%= if !fully_verified and !creation_code(@address) do %>
                    <% path = address_verify_contract_path(@conn, :new, @address.hash) %>
                    <%= link(
                          gettext("Verify & Publish"),
                          to: path,
                          class: "button button-primary button-sm float-right ml-3",
                          "data-test": "verify_and_publish"
                        ) %>
                  <% end %>
                </div>
              </div>
            <% end %>
          <div class="tile tile-muted">
            <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= contract_code %></code></pre>
          </div>
        <% end %>
      </section>

      <%= if smart_contract_verified || (!smart_contract_verified && metadata_for_verification) do %>
        <% target_contract = if smart_contract_verified, do: @address.smart_contract, else: metadata_for_verification %>
        <%= if target_contract.external_libraries && target_contract.external_libraries != [] do %>
          <section>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "External libraries" %></h3>
            </div>
            <div class="tile tile-muted mb-4">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= raw(format_external_libraries(target_contract.external_libraries, @conn)) %></code>
              </pre>
            </div>
          </section>
        <% end %>
      <% end %>
    </div>
  </div>
  <script defer data-cfasync="false" src="<%= static_path(@conn, "/js/address.js") %>"></script>
</section>
